#include <stdio.h>
#include <stdlib.h>

#include "queue.h" 

typedef struct element_s {
	TAILQ_ENTRY(element_s) entries;
	int data;
	char buffer[32];
} element_t;

int main()
{
	element_t *p = NULL;
	element_t *q = NULL;
	element_t *curr = NULL;

	//定义队列
	TAILQ_HEAD(queue_s, element_s) queueHead;
	/***********************************************************************
	**	struct queue_s{ 
        **	struct element_s *tqh_first;
        **	struct element_s **tqh_last;
	**	} queueHead;
	************************************************************************/
	

	//队列初始化
	TAILQ_INIT(&queueHead);
	/***********************************************************************
	**	(&queueHead)->tqh_first = NULL;	
        **	(&queueHead)->tqh_last = &(&queueHead)->tqh_first;	
	************************************************************************/
	
	/***********************************************************************
	**	
        **	
        **	
	**	
	************************************************************************/
	//尾部插入，入队
	p = malloc(sizeof(element_t));
	p->data = 1;
	TAILQ_INSERT_TAIL(&queueHead, p, entries); 
	/***********************************************************************
	**	(p)->entries.tqe_next = NULL;             
	**	(p)->entries.tqe_prev = (head)->tqh_last; 
	**	*(&queueHead)->tqh_last = (p);                
	**	(&queueHead)->tqh_last = &(p)->entries.tqe_next;
	************************************************************************/

	q = malloc(sizeof(element_t));
	q->data = 2;
	TAILQ_INSERT_TAIL(&queueHead, q, entries);

	//遍历队列，输出数据
	TAILQ_FOREACH(curr, &queueHead, entries) {
		printf("%d [%s]\n", curr->data, curr->buffer);
	}   

	//从头部删除，出队
	p = TAILQ_FIRST(&queueHead);
	TAILQ_REMOVE(&queueHead, p, entries);

	//输出队列尾部元素
	p = TAILQ_LAST(&queueHead, queue_s);
	printf("%d [%s]\n", p->data, p->buffer);
	
	exit(EXIT_SUCCESS);
}

